﻿using ASmile.Com;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ASmile.Web
{
    /// <summary>
    /// 页面控制器基类
    /// </summary>
    [UserPageAuthorize]
    public class BasePageController : Controller, ILoginSession, IApiResultExtend
    {
        #region ILoginSession

        LoginUser _CurrUser;

        public LoginUser CurrUser
        {
            get
            {
                if (_CurrUser == null)
                {
                    string token = HttpContext.GetToken();
                    _CurrUser = LoginState.Get(token);
                }
                return _CurrUser;
            }
        }

        string _RemoteIp;
        /// <summary>
        /// 返回远程请求地址
        /// </summary>
        protected string RemoteIp
        {
            get
            {
                if (_RemoteIp == null)
                {
                    _RemoteIp = Request.HttpContext.GetRemoteIp();
                }
                return _RemoteIp;
            }
        }

        public string LoginHost
        {
            get
            {
                if (_RemoteIp == null)
                {
                    _RemoteIp = this.HttpContext.GetRemoteIp();
                }
                return _RemoteIp;
            }
        }

        #endregion
    }

    public class BasePageController<TBLLObj> : BasePageController
        where TBLLObj : ILoginSessionSet, new()
    {
        TBLLObj _BLLObj;

        protected TBLLObj BLLObj
        {
            get
            {
                if (_BLLObj == null)
                {
                    _BLLObj = new TBLLObj();
                    _BLLObj.SetLoginSession(this);
                }
                return _BLLObj;
            }
        }
    }
}
